/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _DAL_FASTDDS_DAL_DATAREADER_IMPL_HPP_
#define _DAL_FASTDDS_DAL_DATAREADER_IMPL_HPP_

#include <mutex>
#include <fastdds/dds/domain/DomainParticipant.hpp>
#include <fastdds/dds/publisher/Publisher.hpp>
#include <fastdds/dds/subscriber/Subscriber.hpp>
#include <fastdds/dds/publisher/DataWriter.hpp>
#include <fastdds/dds/subscriber/DataReader.hpp>
#include "fastdds/dds/core/StackAllocatedSequence.hpp"
#include "dal_fastdds/listener_impl/DataReaderListenerImpl.hpp"

#ifdef DAL_ENABLE_DMABUFHEAP
#include "dal_fastdds/dbh/DalDbhReader.hpp"
#endif

using namespace eprosima::fastdds::dds;

class DalDataReaderImpl
{
public:
enum class ReaderType
{
    DDS_READER,
    DBH_READER,
    MIX_READER
};

public:
    explicit DalDataReaderImpl(DataReader* fastdds_datareader, DataReaderListenerImpl* listener);
    ~DalDataReaderImpl();

    DataReader*& FastddsDataReader();
    void SetFastddsDataReader(DataReader* fastdds_datareader);
    Subscriber*& FastddsSubscriber();
    void SetFastddsSubscriber(Subscriber*& subscriber);
    DomainParticipant*& FastddsDomainParticipant();
    void SetFastddsDomainParticipant(DomainParticipant*& participant);
    void SetTypeSupport(const void* type_support);

    ReturnCode_t DDSTake(void* data, SampleInfoSeq& info);
    ReturnCode_t DDSRead(void* data, SampleInfoSeq& info);

    ReaderType type_ = ReaderType::DDS_READER;
#ifdef DAL_ENABLE_DMABUFHEAP
    ReturnCode_t DBHTake(void* data, SampleInfoSeq& info);
    // used for dmabufheap transfer
    DalDbhReader* dbh_reader_ = nullptr;
#endif

private:
    DataReaderListenerImpl* listener_ = nullptr;
    DataReader* fastdds_datareader_ = nullptr;
    Subscriber* fastdds_sub_ = nullptr;
    DomainParticipant* fastdds_participant_ = nullptr;
    const void* type_support_ = nullptr;

    std::mutex mutex_;
};

#endif  // _DAL_FASTDDS_DAL_DATAREADER_IMPL_HPP_
